cd "~/Dropbox/Shared Folder - J. Ladd & M (1). Meredith/Replication Files/"
cap log close
log using "LogFiles/TableA10.log", replace

clear all
set more off

mat Results = J(10, 9, -9)

use "RawData/GallupDataBJPS.dta", clear

// Drops Unrepresentative Series
drop if drops == "All" | regexm(drops, "Party")
// Drops Obs. w/o Gender
drop if female < 0
// Drops Obs. w/o Weights
drop if final_weight < 0
// Drops Obs. w/o Dem., Ind., or Rep.
keep if party == -1 | party == 0 | party == 1
// Drops Phone Surveys
keep if survey == 1

// Sets the Survey Date as the Mid-Point in Field
gen middle_date = .
replace middle_date = floor((start_date + end_date) / 2)
sort middle_date
format middle_date %td
gen year = year(middle_date)  

// Codes Dem. as 0, Ind. as 1/2, and Rep. as 1
gen partisan = 0 if party == -1
replace partisan = 1 if party == 1
replace partisan = .5 if party == 0

// Aggregates Weighted Partisanship for Females
egen total_partisan_female = sum(partisan * (female == 1) * final_weight), by(series)
// Aggregates Weighted Partisanship for Males
egen total_partisan_male = sum(partisan * (female == 0) * final_weight), by(series)
// Weighted Female Count
egen total_obs_female = sum((female == 1) * (final_weight)), by (series)
// Weighted Male Count
egen total_obs_male = sum((female == 0) * (final_weight)), by (series)
// Avg. Female Partisanship
gen partisan_female = total_partisan_female / total_obs_female
// Avg. Male Partisanship
gen partisan_male = total_partisan_male / total_obs_male 
// Difference in Female and Male Avg. Partisanship
gen partisan_difference = partisan_female - partisan_male

// Only Keeps One Obs. Per Survey
keep if obs_num == 1 

// Run Linear RDD in In-Person Surveys in Presidential Eletion Years from 1960 though 1992
// Bandwidth is +/- 4 Years of 1/1 of Presidential Election Year

forvalues i = 1960(4)1992 {
gen post`i' = (middle_date >= mdy(1, 1, `i')) 
gen forcing = (middle_date - mdy(1, 1, `i')) / 365.25
gen post`i'Xforcing = post`i' * forcing
regress partisan_difference forcing post`i'Xforcing post`i' if year >= `i' - 4 & year <= `i' + 3 , robust
mat Results[1, 1 + ((`i' - 1960) / 4)] = _b[_cons]  
mat Results[2, 1 + ((`i' - 1960) / 4)] = -_se[_cons]  
mat Results[3, 1 + ((`i' - 1960) / 4)] = _b[post`i']  
mat Results[4, 1 + ((`i' - 1960) / 4)] = -_se[post`i']  
mat Results[5, 1 + ((`i' - 1960) / 4)] = _b[forcing]  
mat Results[6, 1 + ((`i' - 1960) / 4)] = -_se[forcing] 
mat Results[7, 1 + ((`i' - 1960) / 4)] = _b[post`i'Xforcing]  
mat Results[8, 1 + ((`i' - 1960) / 4)] = -_se[post`i'Xforcing] 
test post`i'Xforcing = post`i' = 0 
mat Results[9, 1 + ((`i' - 1960) / 4)] = r(p)  
mat Results[10, 1 + ((`i' - 1960) / 4)] = e(N)
drop forcing post`i'Xforcing post`i'
}

matlist Results

log close
